<template>
    <div class="permission-container">
        <el-card class="permission-card" shadow="hover" v-loading="loading">
            <div slot="header" class="card-header">
                <span class="header-title">权限管理系统</span>
                <el-button class="header-button" type="primary" size="small" icon="el-icon-plus" v-auth="'add'"
                    @click="handleCreate">
                    添加权限
                </el-button>
            </div>

            <div class="card-content">
                <el-tree class="permission-tree" :data="data" :props="defaultProps" node-key="id" default-expand-all>

                    <div class="custom-tree-node" slot-scope="{ node, data }">
                        <div class="node-content">
                            <el-tag :type="enumObj[data.type].type" size="small" effect="plain">
                                {{ enumObj[data.type].label }}
                            </el-tag>
                            <span class="node-label">{{ node.label }}</span>
                        </div>

                        <div class="node-actions">
                            <el-button v-if="data.type != 2" type="text" size="mini" icon="el-icon-plus"
                                @click.stop="append(data)" v-auth="'add'">
                            </el-button>
                            <el-button type="text" size="mini" icon="el-icon-edit" v-auth="'edit'"
                                @click.stop="edit(node, data)">
                            </el-button>
                            <el-button type="text" size="mini" icon="el-icon-delete" v-auth="'del'"
                                @click.stop="remove(node, data)" style="color: #F56C6C">
                            </el-button>
                        </div>
                    </div>
                </el-tree>
            </div>
        </el-card>
        <AddOrEdit ref="AddOrEditRef" :handleType="handleType" :title="title" :type="authType" :loading="loading"
            :disable="disableFormItem" :visible="dialogVisible" :form="form" :isEdit="isEdit" @submit="submit"
            @closeModal="close" />

    </div>
</template>

<script>
/**
 * 新增按钮时 给用户输入的code值加上当前路由hash前缀，用于区分按钮权限
 */
import { permissionAdd, permissionDel, permissionEdit, permissionTree } from '../api/product';
import AddOrEdit from './AddOrEdit.vue';
const defaultForm = {
    permissionName: null,
    permissionDesc: null,
    uri: null,
    code: null,
    path: null,
    title: null,
    icon: null,
    visible: 1,
    sort: null,
}
// 0 menu菜单  1 page页面 2 button按钮 
const enumObj = {
    2: {
        type: 'primary',
        label: '按钮'
    },
    1: {
        type: 'success',
        label: '页面'
    },
    0: {
        type: 'warning',
        label: '菜单'
    }
}
export default {
    data() {
        return {
            enumObj: Object.freeze(enumObj),
            loading: false,
            editInfo: null,
            authType: '',
            dialogVisible: false,
            isEdit: false,
            handleType: 0,
            data: [],
            defaultProps: {
                children: 'children',
                label: 'title'
            },
            disableFormItem: false,
            form: defaultForm,
            parent_id: null,
            buttonCodePrefix: ''
        };
    },
    filters: {
        expand() {

        }
    },
    computed: {
        title() {
            let t = this.isEdit ? '修改' : '新增';
            t += enumObj[this.handleType]?.label;
            return t
        }
    },
    components: { AddOrEdit },
    created() {
        this.getList();
    },
    methods: {
        // 新增菜单
        handleCreate() {
            this.parent_id = 0;
            this.handleType = 0;
            this.$refs.AddOrEditRef.fillForm(defaultForm);
            this.isEdit = false;
            this.dialogVisible = true;
            console.log('新增菜单', this.handleType);

        },
        // 新增页面/按钮
        append(data) {
            this.parent_id = data.id;
            // console.log(data, '新增页面/按钮');
            this.$refs.AddOrEditRef.fillForm(defaultForm);
            this.handleType = data.type == 0 ? 1 : 2;
            // 页面只能新增按钮
            this.disableFormItem = data.type == 1;
            this.authType = data.path;//
            this.isEdit = false;
            this.dialogVisible = true;
            // console.log('this.handleType', this.handleType);
            if (data.type == 1) {
                // 
                this.buttonCodePrefix = data.path;
            }
        },
        edit(node, data) {
            // console.log('edit', data);

            this.authType = data.type == 1 ? node.parent.data.path : data.path//
            this.handleType = data.type;
            this.editInfo = data;
            let fillData = {
                ...data,
                // status: !!data.status
            };
            this.disableFormItem = data.type == 2;
            if (data.type == 2) {
                fillData = {
                    ...fillData,
                    path: node.parent.data.path
                }
            }
            this.$refs.AddOrEditRef.fillForm(fillData);
            this.isEdit = true;
            this.dialogVisible = true;
        },
        remove(node, data) {
            this.handleType = data.type;
            this.$confirm(`确认删除该${enumObj[this.handleType].label}吗?`, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                // 删除逻辑
                this.loading = true;
                permissionDel(data.id).then(res => {
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    })
                    this.getList();
                }).catch(err => {

                }).finally(() => {
                    this.loading = false;
                })
            }).catch(() => { })
        },
        handleParams(data) {
            let params = {};
            if (this.isEdit) {
                params = {
                    ...this.editInfo,
                    ...data,
                }
            } else {
                params = {
                    parentId: this.parent_id,
                    type: this.handleType,
                    // component: data.page,
                    // always_show: 1,
                    // keep_alive: 0,
                    ...data,
                }
                if (this.handleType == 2) {
                    params.code = this.buttonCodePrefix + ":" + params.code
                    params.title = data.permissionName
                } else {
                    params.code = params.path;
                }
            }
            return params;
        },
        async submit(data) {
            let p = this.handleParams(data);
            this.loading = true;
            try {
                let res = this.isEdit ? await permissionEdit(p) : await permissionAdd(p);
                let msg = res.message || `${this.isEdit ? '修改' : '新增'}成功!`;
                this.$message({
                    type: 'success',
                    message: msg
                })
                this.dialogVisible = false;
                this.getList();
            } catch (e) {
                this.loading = false;
            }
        },
        close() {
            // console.log('close');
            this.dialogVisible = false;
        },
        getList() {
            this.loading = true;
            permissionTree().then(res => {
                this.data = res;
            }).finally(() => {
                this.loading = false;
            })
        },
    }
}
</script>

<style lang="scss" scoped>
.permission-container {
    height: 100%;
    .permission-card {
        width: 100%;
        border-radius: 4px;
        border: none;

        .card-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 15px 0px;

            .header-title {
                font-size: 18px;
                font-weight: 600;
                color: #303133;
            }

            .header-button {
                border-radius: 4px;
                padding: 7px 15px;
            }
        }

        .card-content {
            height: calc(90vh - 200px);
            overflow-y: auto;
            overflow-x: hidden;
            
            // 自定义滚动条样式
            &::-webkit-scrollbar {
                width: 6px;
            }
            
            &::-webkit-scrollbar-track {
                background: #f1f1f1;
                border-radius: 3px;
            }
            
            &::-webkit-scrollbar-thumb {
                background: #c1c1c1;
                border-radius: 3px;
                
                &:hover {
                    background: #a8a8a8;
                }
            }
        }


    }

    .permission-tree {
        padding: 0 15px;
        height: 100%;
        
        :deep .el-tree {
            height: 100%;
        }

        :deep .el-tree-node__content {
            height: 40px;
            margin: 4px 0;
        }

        .custom-tree-node {
            flex: 1;
            display: flex;
            align-items: center;
            font-size: 14px;
            padding-right: 15px;

            .node-content {
                display: flex;
                align-items: center;

                .el-tag {
                    margin-right: 10px;
                    border-radius: 4px;
                }

                .node-label {
                    font-weight: 500;
                    color: #606266;
                }
            }

            .node-actions {
                opacity: 0;
                transition: opacity 0.3s;

                .el-button {
                    padding: 5px;
                    margin-left: 5px;

                    &:hover {
                        background-color: #f5f7fa;
                        border-radius: 4px;
                    }
                }
            }

            &:hover .node-actions {
                opacity: 1;
            }
        }
    }
}
</style>